package stream;
import method_references.Employee;
import method_references.EmployeeData;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mubei
 * @date 2021/11/24
 * @description  测试终止
 */
public class StreamAPITest2 {

    //归约
    @Test()
    public void test1() {
        //reduce(T identity, BinaryOperator)  可以将流中元素反复结合起来 得到一个值  返回一个t
        //计算1  计算1-10的自然数的和
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        System.out.println(list.stream().reduce(0, Integer::sum));

        //reduce(BinaryOperator)  将流中数据反复结合起来  计算员工总和
        List<Employee> employees = EmployeeData.getAllEmployees();
        System.out.println(employees.stream().map(Employee::getSalary).reduce(Double::sum));
    }


    @Test()
    public void test3() {
        //collect(Collector c)  收集  将流转化成其他形式
        List<Employee> employees = EmployeeData.getAllEmployees();
        List<Employee> list = employees.stream().filter(e -> e.getSalary() > 8999).collect(Collectors.toList());
        list.forEach(System.out::println);
    }
}
